﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace Kyoh.Lib.WinForms
{
    partial class FormPlus
    {
        private class FormPlusSettings
        {
            /// <summary><see cref="Size"/> を記録するかどうかを定義するアプリケーション設定の項目名。</summary>
            public const string SettingsNameMemorizeWindowSize = "MemorizeWindowSize";
            /// <summary><see cref="Size"/> のアプリケーション設定の項目名。</summary>
            public const string SettingsNameWindowSize = "WindowSize";
            /// <summary><see cref="FormWindowState"/> のアプリケーション設定の項目名。</summary>
            public const string SettingsNameWindowState = "WindowState";
            /// <summary>通知アイコンに最小化するかどうかを定義するのアプリケーション設定の項目名。</summary>
            public const string SettingsNameMinimizeToNotifier = "MinimizeToNotifier";
            /// <summary>フォームを閉じる動作を定義するのアプリケーション設定の項目名。</summary>
            public const string SettingsNameFormClosingBehavior = "FormClosingBehavior";
            private readonly SettingsBase _settings;
            private readonly Dictionary<string, Type> _properties;

            /// <summary><see cref="FormPlus"/> クラスの新しいインスタンスを初期化します。</summary>
            /// <param name="settings">フォームの設定値を保存する <see cref="ApplicationSettingsBase"/>。</param>
            public FormPlusSettings(SettingsBase settings)
            {
                Debug.WriteLine("FormPlusSettings::BeginInit" + DateTime.Now);
                // settings.PropertyValuesで必要な項目があるかどうかチェック
                _properties =
                    new Dictionary<string, Type>
                    {
                        {SettingsNameMemorizeWindowSize, typeof (bool)},
                        {SettingsNameWindowSize, typeof (Size)},
                        {SettingsNameWindowState, typeof (FormWindowState)},
                        {SettingsNameMinimizeToNotifier, typeof (bool)},
                        {SettingsNameFormClosingBehavior, typeof (FormClosingBehavior)}
                    };

                if (settings != null)
                {
                    foreach (SettingsProperty property in settings.Properties)
                        if (_properties.ContainsKey(property.Name) && _properties[property.Name] == property.PropertyType)
                            _properties[property.Name] = null;
                }
                //TODO: nullじゃないプロパティが残っていたら例外
                _settings = settings;
                Debug.WriteLine("FormPlusSettings::EndInit" + DateTime.Now);
            }

            private T GetSettings<T>(string settingName)
            {
                Debug.WriteLine("GetSettings::" + settingName + DateTime.Now);
                return _settings != null && _properties[settingName] == null ? (T)_settings[settingName] : default(T);
            }

            private bool SetSettings<T>(string settingName, T settingValue)
            {
                Debug.WriteLine("SetSettings::" + settingName + DateTime.Now);
                if (_settings != null && _properties[settingName] == null)
                {
                    _settings[settingName] = settingValue;
                    return true;
                }
                else
                    return false;
            }

            internal bool SettingsMemorizeWindowSize { get { return GetSettings<bool>(SettingsNameMemorizeWindowSize); } set { SetSettings(SettingsNameMemorizeWindowSize, value); } }
            internal Size SettingsWindowSize { get { return GetSettings<Size>(SettingsNameWindowSize); } set { SetSettings(SettingsNameWindowSize, value); } }
            internal FormWindowState SettingsWindowState { get { return GetSettings<FormWindowState>(SettingsNameWindowState); } set { SetSettings(SettingsNameWindowState, value); } }
            internal bool SettingsMinimizeToNotifier { get { return GetSettings<bool>(SettingsNameMinimizeToNotifier); } set { SetSettings(SettingsNameMinimizeToNotifier, value); } }
            internal FormClosingBehavior SettingsFormClosingBehavior { get { return GetSettings<FormClosingBehavior>(SettingsNameFormClosingBehavior); } set { SetSettings(SettingsNameFormClosingBehavior, value); } }

            internal void Save()
            {
                _settings.Save();
            }
        }
    }
}